package com.wonders.frame.autoGeneration.controller;

import com.gcr.server.response.Response;
import com.gcr.util.StringUtil;
import com.wonders.frame.autoGeneration.support.enums.DatasourceOperatorEnum;
import com.wonders.frame.autoGeneration.support.enums.TableInfoSqlTypeEnum;
import com.wonders.frame.autoGeneration.support.from.TableForm;
import com.wonders.frame.controller.core.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tableInfo")
public class TableInfoController extends AbstractController {

    @Autowired
    private JdbcTemplate jt;

//    Microsoft SQL Server
//    MySQL

    @RequestMapping("getTables")
    public Response getTables(@RequestBody TableForm form){
        String sql = DatasourceOperatorEnum.getOperator().getSql(TableInfoSqlTypeEnum.getTables);
        return successResponse(jt.queryForList(sql,DatasourceOperatorEnum.getOperator()
                .getParams(TableInfoSqlTypeEnum.getTables, form)));
    }
    @RequestMapping("getSchemas")
    public Response getSchemas(){
        String sql = DatasourceOperatorEnum.getOperator().getSql(TableInfoSqlTypeEnum.getSchemas);
        if(StringUtil.isEmpty(sql)){
            return errorResponse();
        }
        return successResponse(jt.queryForList(sql));
    }
    @RequestMapping("getColumn")
    public Response getColumn(@RequestBody TableForm form){
        String sql = DatasourceOperatorEnum.getOperator().getSql(TableInfoSqlTypeEnum.getColumn);
        return successResponse(jt.queryForList(sql,DatasourceOperatorEnum.getOperator()
                .getParams(TableInfoSqlTypeEnum.getColumn, form)));
    }


}
